//
//  FactPage.m
//  WorldOfFacts_V2
//
//  Created by Demjanko Denis on 03.12.12.
//  Copyright (c) 2012 it-dimension. All rights reserved.
//

#import "FactPage.h"
#import "MGBox.h"
#import "MGStyledBox.h"
#import "MGBoxLine.h"
#import "DataBase.h"
#import "ButtonPhotoBox.h"
#import "UIButton+WebCache.h"
#import "UIImageExtras.h"

@implementation FactPage

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addBoxes];
    }
    return self;
}

-(void)addBoxes{    
    boxScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.frame.size.height)];   
    boxScroller.alwaysBounceVertical = YES;
    boxScroller.scrollEnabled = TRUE;
    [self addSubview:boxScroller];
        
    photosView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5, 320, 120)];
    photosView.pagingEnabled = YES;
    photosView.delegate = self;
    photosView.showsVerticalScrollIndicator = FALSE;
    photosView.showsHorizontalScrollIndicator = FALSE;
    [boxScroller addSubview:photosView];
    
    pageControl = [[OMPageControl alloc] initWithFrame:CGRectMake(10, 130, 300, 20)];
    pageControl.numberOfPages = 0;
    pageControl.currentPage = 0;
    pageControl.userInteractionEnabled = FALSE;
    pageControl.imageNormal = [UIImage imageNamed:@"pageDot.png"];
    pageControl.imageCurrent = [UIImage imageNamed:@"currentPageDot.png"];
    [boxScroller addSubview:pageControl];
    
    description = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 300, 20)];
    description.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:16];
    description.backgroundColor = [UIColor clearColor];
    description.textColor = [UIColor blackColor];
    description.numberOfLines = 0;
    description.shadowColor = [UIColor whiteColor];
    description.shadowOffset = CGSizeMake(0, 1);
    [boxScroller addSubview:description];
    
    category = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 300, 20)];
    category.font = [UIFont fontWithName:@"HelveticaNeue-Italic" size:16];
    category.backgroundColor = [UIColor clearColor];
    category.textColor = [UIColor blackColor];
    category.numberOfLines = 0;
    category.shadowColor = [UIColor whiteColor];
    category.shadowOffset = CGSizeMake(0, 1);
    [boxScroller addSubview:category];
    
    separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comments_separator.png"]];
    separator.frame = CGRectMake(10, 200, 300, 1);
    [boxScroller addSubview:separator];
}

-(void)updateScrollerHeight:(NSNumber*)offset{
    boxScroller.contentSize = CGSizeMake(0, boxScroller.contentSize.height+offset.intValue);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGRect visibleBounds = scrollView.bounds;
	int index = (int)(floorf(CGRectGetMidX(visibleBounds) / CGRectGetWidth(visibleBounds)));
    pageControl.currentPage = index;
}

#pragma mark - content

-(void)setContent:(NSMutableDictionary*)fact{
    category.text = [[[DataBase sharedManeger] getCategoryName:[fact valueForKey:@"category_id"]] lowercaseString];

    description.text = [fact valueForKey:@"full_description"];
    [description sizeToFit];
    
    //remove all photos
    for (UIView *view in photosView.subviews) {
        [view removeFromSuperview];
    }
    
    //add photos
    NSMutableArray *images = [fact objectForKey:@"images"];
    
    float xSize = 0;
    int count = images.count;
    
    if(count==1){
        pageControl.hidden = YES;
        
        ButtonPhotoBox *photoBox = [[ButtonPhotoBox alloc] initWithFrame:CGRectMake(10, 5, 300, 200)];
        [photoBox download:[images objectAtIndex:0]];
        [photoBox setTag:50];
        [photoBox addTarget:self action:@selector(onPhoto:) forControlEvents:UIControlEventTouchUpInside];
        [photosView addSubview:photoBox];
    }
    else{
        pageControl.hidden = FALSE;

        float xPosition = 10;
        int pages = 0;
        for (int i=0; i<images.count; i++) {        
            ButtonPhotoBox *photoBox = [[ButtonPhotoBox alloc] initWithFrame:CGRectMake(xPosition, 5, 140, 120)];
            photoBox.tag = i+50;
            [photoBox download:[images objectAtIndex:i]];
            [photoBox addTarget:self action:@selector(onPhoto:) forControlEvents:UIControlEventTouchUpInside];
            [photosView addSubview:photoBox];
            
            if(i%2==0){
                xSize += 320;
                pages++;
            }
            
            xPosition += 160;
        }
        
        pageControl.numberOfPages = pages;

    }

    //configure scroll
    photosView.contentSize = CGSizeMake(xSize, 0);
    
    [boxScroller setContentOffset:CGPointMake(0, 0)];
     
    if(count==1){
        category.frame = CGRectMake(category.frame.origin.x, description.frame.size.height+235, category.frame.size.width, category.frame.size.height);
        separator.frame = CGRectMake(10, description.frame.size.height+228, 300, 2);
        description.frame = CGRectMake(description.frame.origin.x, 220, description.frame.size.width, description.frame.size.height);
        photosView.frame = CGRectMake(photosView.frame.origin.x, photosView.frame.origin.y, photosView.frame.size.width, 220);
        boxScroller.contentSize = CGSizeMake(0, description.frame.size.height+375);
    }
    else{
        category.frame = CGRectMake(category.frame.origin.x, description.frame.size.height+167, category.frame.size.width, category.frame.size.height);
        separator.frame = CGRectMake(10, description.frame.size.height+160, 300, 2);
        description.frame = CGRectMake(description.frame.origin.x, 150, description.frame.size.width, description.frame.size.height);
        photosView.frame = CGRectMake(photosView.frame.origin.x, photosView.frame.origin.y, photosView.frame.size.width, 130);
        boxScroller.contentSize = CGSizeMake(0, description.frame.size.height+300);
    }
}

#pragma mark - IBActions

-(void)onPhoto:(UIButton*)sender{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ON_PHOTO_CLICKED" object:[NSNumber numberWithInt:sender.tag]];
}

#pragma mark - memory

-(void)dealloc{
    photosView = nil;
    boxScroller = nil;
    pageControl = nil;
}

@end
